package com.sharyuke.utils

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseSectionQuickAdapter
import com.chad.library.adapter.base.entity.SectionEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class EaseViewHolder<T : SectionEntity>(view: View) : BaseViewHolder(view) {
    var item: T? = null

    fun onHasTitle(block: T.() -> Unit) = item?.isHeader?.ifTrue { item?.apply(block) }
    fun onHasItem(block: T.() -> Unit) = item?.isHeader?.ifFalse { item?.apply(block) }
}

fun <T : SectionEntity> adapterSection(layoutHeader: Int, layoutItem: Int, data: List<T>? = null, convert: EaseViewHolder<T>.() -> Unit) = object : BaseSectionQuickAdapter<T, EaseViewHolder<T>>(layoutHeader, data?.toMutableList()) {
    init {
        setNormalLayout(layoutItem)
    }

    override fun convert(holder: EaseViewHolder<T>, item: T) {
        holder.item = item
        convert(holder)
    }

    override fun convertHeader(helper: EaseViewHolder<T>, item: T) {
        helper.item = item
        convert(helper)
    }
}

fun <T : SectionEntity> BaseSectionQuickAdapter<T, EaseViewHolder<T>>.withRecyclerView(recyclerView: RecyclerView) = apply { recyclerView.adapter = this }
